package com.gimi.cloud.bbp.task.service.mall.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alipay.api.AlipayApiException;
import com.alipay.api.domain.Data;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.gimi.cloud.bbp.common.enums.AliPayTradeStatusEnum;
import com.gimi.cloud.bbp.common.enums.CommonEnume.OrderStatus;
import com.gimi.cloud.bbp.common.enums.CommonEnume.PayChannel;
import com.gimi.cloud.bbp.commonService.alipay.AliPayService;
import com.gimi.cloud.bbp.dao.dao.mall.MallPaymentQueueDao;
import com.gimi.cloud.bbp.dao.dao.order.OrderDao;
import com.gimi.cloud.bbp.entity.mall.MallPaymentQueueEntity;
import com.gimi.cloud.bbp.entity.order.OrderEntity;
import com.gimi.cloud.bbp.task.service.mall.MallPaymentQueueTaskService;

import tk.mybatis.mapper.entity.Example;

/**
 * 支付宝定时服务
 * @author 李林峰
 * @version 1.0
 * @Description
 * @create 2018年2月6日
 */
@Service
public class MallPaymentQueueTaskServiceImpl implements MallPaymentQueueTaskService {
	
	private static final Logger log = LoggerFactory.getLogger(MallPaymentQueueTaskServiceImpl.class);
	
	@Autowired AliPayService aliPayService;
	@Autowired MallPaymentQueueDao mallPaymentQueueDao;
	@Autowired OrderDao orderDao;

	@Override
	public void queryTask() {
		log.info("开始处理支付队列");
		Example example = new Example(MallPaymentQueueEntity.class);
		example.createCriteria().andEqualTo("pdPayStatus", 0).andLessThanOrEqualTo("pdExpectedEndTime", new Data());
		
		for (MallPaymentQueueEntity mallPaymentQueueEntity : mallPaymentQueueDao.selectByExample(example)) {
			log.info("开始查询支付状态");
			PayChannel payChannel = PayChannel.valueOf(mallPaymentQueueEntity.getPdPayChannel());
			String orderCode = mallPaymentQueueEntity.getOrderCode();
			Integer pdPayStatus = null;
			switch (payChannel) {
			case ZFB:
				pdPayStatus = aliPaymentQueueQuery(orderCode);
				break;
			default:
				break;
			}
			
			if (pdPayStatus==null) {
				break;
			}
			
			log.info("保存支付状态");
			OrderEntity record = new OrderEntity();
			switch (pdPayStatus) {
			case 1:
				record.setStatus(OrderStatus.L04.name());
				break;
			case 2:
				record.setStatus(OrderStatus.L09.name());
				break;
			default:
				break;
			}
			Example updataOrderExample = new Example(OrderEntity.class);
			updataOrderExample.createCriteria().andEqualTo("orderCode", orderCode);
			orderDao.updateByExampleSelective(record, updataOrderExample);
			
			mallPaymentQueueEntity.setPdPayStatus(pdPayStatus);
			mallPaymentQueueDao.updateByPrimaryKeySelective(mallPaymentQueueEntity);
		}
		log.error("处理支付队列结束");
	}

	private Integer aliPaymentQueueQuery(String out_trade_no) {
		try {
			AlipayTradeQueryResponse alipayTradeQuery = aliPayService.alipayTradeQuery(out_trade_no, null);
			switch (AliPayTradeStatusEnum.valueOf(alipayTradeQuery.getTradeStatus())) {
			case TRADE_SUCCESS:return 1;
			case TRADE_CLOSED:
			case TRADE_FINISHED:return 2;
			default:
				return null;
			}
		} catch (AlipayApiException e) {
			log.error("支付宝查询异常");
			e.printStackTrace();
		}
		return null;
	}

}
